package com.eascs.ant.data.controller;

import java.util.Map;

import javax.inject.Inject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.ant.common.controller.BaseMasterViewController;
import com.eascs.ant.data.entity.hr.HrFavorite;
import com.eascs.ant.data.entity.hr.HrSearchForm;
import com.eascs.ant.data.service.HrService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.web.context.RequestContext;
import com.eascs.web.sso.UserHolder;

@Controller
public class HrViewController extends BaseMasterViewController{

	private static Logger logger = LoggerFactory.getLogger(HrViewController.class);
	@Inject
	private HrService hrService;
	
	@RequestMapping("/hr")
	public String toHrIndex() {
		//进入舆情页面，先检索出各平台最新新闻
		return "/home/hr";
	}
	
	@RequestMapping("/hr/query")
	public String getResumeTest(HrSearchForm form) {
		String account = UserHolder.getUser().getAccount();
		logger.info("操作员：{},查询表单入参：{}", account ,JSON.toJSONString(form));
		PaginationInfo paginationInfo = hrService.queryByPage(form);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setUrl(UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "pageIndex", ""));
		paginationInfo.setFirstRenderColumnIndex(0);

		ViewContext.put("paginationInfo", paginationInfo);
		return "/view/hr/hr_result";
	}
	
	
	@RequestMapping("/hr/queryById")
	public String getResume(String id){
		String account = UserHolder.getUser().getAccount();
		logger.info("操作员：{},查询单份简历id：{}", account ,id);
		Map<String, Object> data = hrService.queryById(id);
		
		String html = String.valueOf(data.get("html"));
		String url = String.valueOf(data.get("简历链接"));
		//过滤掉html中link及script
		html = html.replaceAll("<link .*?/>", "")
				.replaceAll("<script.*?script>", "").
				replaceAll("<p class=\'rself_list.*?</p>", "");
		
		Document doc = Jsoup.parse(html);
		StringBuilder appendHtml = new StringBuilder(); 
		appendHtml.append("<div><a id=\"url\" href=\"#\" rel=\"nofollow\">简历链接:"+url.replaceAll("&", "%26")+"</a></div>");
		appendHtml.append("<div><input class=\"action-ajax-btn \" href=\"/hr/addFr?url="+url.replaceAll("&", "%26")+"\" type=\"button\" id=\"sc\" value=\"收藏该简历\" onclick=\"scjl()\"></input></div>");
        doc.getElementById("divHead").append(appendHtml.toString());
		ViewContext.put("html", doc.html());
		return "/view/hr/hr_resumeDetail";
	}
	
	
	@RequestMapping("/hr/qrfr")
	public String queryFavorite(String url){
		String account = UserHolder.getUser().getAccount();
		HrFavorite fr = hrService.queryFavorite(account);
		ViewContext.put("fr", fr);
		if(fr != null) {
			ViewContext.put("menus", fr.getMenuList());
		}
		return "/view/hr/hr_favorite";
	}
}
